package com.gree.elasticsearch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.AdviceMode;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@SpringBootApplication
@EnableTransactionManagement(mode = AdviceMode.PROXY, proxyTargetClass = false)
@EnableWebSecurity
@EnableGlobalMethodSecurity(mode = AdviceMode.PROXY,  prePostEnabled = true)
public class ElasticsearchApplication {

    @Value("spring.datasource.url")
    String jdbcUrl;

    @Value("spring.datasource.username")
    String username;

    @Value("spring.datasource.password")
    String password;

    private Logger log = LoggerFactory.getLogger(ElasticsearchApplication.class.getName());

    /**
     * DataSourceTransactionManager支持嵌套事务,而JpaTransactionManager不支持嵌套事务。在使用
     * DataSourceTransactionManager时候,需要结合JdbcTemplate接口来验证嵌套事务.
     * @param args
     */
//    @Bean(value = "transactionManager")
//    public PlatformTransactionManager platformTransactionManager(DataSource dataSource) {
//        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
//        dataSourceTransactionManager.setDataSource(dataSource);
//        dataSourceTransactionManager.setNestedTransactionAllowed(Boolean.TRUE);
//        return dataSourceTransactionManager;
//    }

    public static void main(String[] args) {
        SpringApplication.run(ElasticsearchApplication.class, args);
    }
}
